﻿
(function () {

    appModule.controller('tenant.views.instructions.index', [
        '$scope', '$uibModal', 'uiGridConstants', 'abp.services.app.instructions',
        function ($scope, $modal, uiGridConstants, instructionsService) {
            var vm = this;

            $scope.$on('$viewContentLoaded', function () {
                Metronic.initAjax();
            });

            vm.loading = false;
            vm.filterText = null;
            vm.curInstructions = null;
            vm.permissions = {
                create: abp.auth.hasPermission('Pages.Tenant.Instructions.Create'),
                edit: abp.auth.hasPermission('Pages.Tenant.Instructions.Update'),
                'delete': abp.auth.hasPermission('Pages.Tenant.Instructions.Delete')
            };

            var urlInstructions = "/api/services/app/instructions/";

            var mainGridDataSource = new kendo.data.DataSource({
                type: 'aspnetmvc-ajax',
                transport: {
                    read: {
                        url: urlInstructions + "KendoGetInstructionss",
                        contentType: "application/json",
                        type: "POST"
                    },
                    parameterMap: function (options, operation) {
                        return JSON.stringify(options);
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.data;
                        }
                        return {};
                    },
                    total: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.total;
                        }
                        return 0;
                    },
                    model: {
                        id: "id",
                        fields: {
                            id: { type: "number" },
                        }
                    }
                }
            });

            vm.mainGridOptions = {
                dataSource: mainGridDataSource,
                dataBound: function () {
                    this.expandRow(this.tbody.find("tr.k-master-row").first());
                },
                columnMenu: true,
                reorderable: true,
                resizable: true,
                filterable: true,
                scrollable: true,
                selectable: "multiple",
                allowCopy: true,
                //height: 650,
                change: function (e) {
                    var selectedRows = this.select();
                    for (var i = 0; i < selectedRows.length; i++) {
                        vm.curInstructions = this.dataItem(selectedRows[i]);
                    }
                },
                pageable: {
                    buttonCount: 5.0,
                    pageSize: 10,
                    pageSizes: [10, 20, 50, 100],
                    refresh: true
                },
                sortable: {
                    mode: "multiple"
                },
                messages: {
                    commands: {
                        excel: "导出"
                    }
                },
                columns: [
                    {
                        title: 'ID',
                        field: 'id'
                    },
                    {
                        title: '标题',
                        field: 'name'
                    },
                    {
                        title: '内容',
                        field: 'content'
                    },
                    {
                        title: '创建时间',
                        field: 'creationTime',
                        format: "{0:yyyy-MM-dd}",
                    },
                    {
                        title: '最后修改时间',
                        field: 'lastModificationTime',
                        format: "{0:yyyy-MM-dd}",
                    },
                ]
            };
            function openCreateOreditInstructionsModal(instructionsId) {
                var modalInstance = $modal.open({
                    templateUrl: '~/App/tenant/views/instructions/createOrEditInstructions.cshtml',
                    controller: 'tenant.views.instructions.createOrEditInstructions as vm',
                    backdrop: 'static',
                    resolve: {
                        instructionsId: function () {
                            return instructionsId;
                        }
                    }
                });

                modalInstance.result.then(function (result) {
                    vm.mainGridOptions.dataSource.read();
                });
            }
            vm.editInstructions = function () {
                if (vm.curInstructions) {
                    openCreateOreditInstructionsModal(vm.curInstructions.id);
                }else{
                    abp.message.error("请选择要修改的行!");
                }
            };

            vm.createInstructions = function () {
                openCreateOreditInstructionsModal(null);
            };

            vm.deleteInstructions = function () {
                if (vm.curInstructions != null && vm.curInstructions.id > 0) {
                    abp.message.confirm(
                        "是否要删除此条记录！",
                        function (isConfirmed) {
                            if (isConfirmed) {
                                instructionsService.deleteInstructions({ id: vm.curInstructions.id }).success(function () {
                                    abp.notify.info(app.localize('SavedSuccessfully'));
                                    vm.mainGridOptions.dataSource.read();
                                }).finally(function () {

                                });
                            }
                        }
                    );
                } else {
                    abp.message.error("请选择要删除的行");
                }
            };

            var urlFiles = "/api/services/app/instructions/";
            vm.detailGridOptions = function (dataItem) {
                return {
                    dataSource: {
                        type: 'aspnetmvc-ajax',
                        transport: {
                            read: {
                                url: urlFiles + "KendoGetInstructionsById",
                                contentType: "application/json",
                                type: "POST"
                            },
                            parameterMap: function (options, operation) {
                                return JSON.stringify(options);
                            }
                        },
                        pageSize: 10,
                        serverPaging: true,
                        serverFiltering: true,
                        serverSorting: true,
                        schema: {
                            data: function (data) {
                                if (data.success === true && data.result != null) {
                                    return data.result.data;
                                }
                                return {};
                            },
                            total: function (data) {
                                if (data.success === true && data.result != null) {
                                    return data.result.total;
                                }
                                return 0;
                            },
                            model: {
                                id: "id",
                                fields: {
                                    isMainFile: { type: "bool" },
                                }
                            }
                        },
                        filter: {
                            logic: "and",
                            filters: [
                                { field: "id", operator: "eq", value: dataItem.id }
                            ]
                        }

                    },
                    columnMenu: true,
                    reorderable: true,
                    resizable: true,
                    filterable: true,
                    scrollable: false,
                    //selectable: "multiple cell",
                    allowCopy: true,
                    pageable: false,
                    sortable: {
                        mode: "multiple"
                    },
                    columns: [
                    {
                        title: '文件名',
                        field: 'name',
                        template: '<a href="' + abp.appPath + 'Profile/DownloadTempFile?id=#=id#"><span>#=name#</span></a>'
                    },
                    {
                        title: '操作',
                        command: {
                            template: '<kendo-button ng-click="vm.deleteFiled(dataItem);" ><span class="k-icon k-delete"></span> 删除</kendo-button>'
                        }
                    }
                    ]
                };
            };

            //删除文件
            vm.deleteFiled = function (dataItem) {
                instructionsService.deleteFiled({ id: dataItem.id }).success(function () {
                    abp.notify.info('删除成功');
                    vm.mainGridOptions.dataSource.read();
                }).finally(function () {

                });
            };
        }]);
})();
